package com.bsh.controller.front;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.Message;
import com.bsh.service.MessageService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 系统消息
 * 
 * @author Administrator
 *
 */
@Controller
public class MessageController {
	private static Logger log = LoggerFactory.getLogger(MessageController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private MessageService messageServ;
	@Autowired
	CommonsMultipartResolver multipartResolver;

	/**
	 * 首页轮播图列表
	 */
	@RequestMapping(value = "/front/message/list")
	public ModelAndView bannerList() {
		String title = request.getParameter("title") == null ? null : request.getParameter("title").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/messageList");
		

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = messageServ.getPage(pager, title);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("title", title);
		mv.addObject("type", "index");
		return mv;
	}
	

	/**
	 * 修改首页banner--初使化
	 */
	@RequestMapping(value = "/front/message/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/messageEdit");
		Message banner = messageServ.getMessage(id);
		mv.addObject("message", banner);
		return mv;
	}

	/**
	 * 修改首页banner--保存
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "/front/message/edit/save")
	public ModelAndView editSave(@ModelAttribute Message banner){
		Integer res = messageServ.editMessage(banner);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/messageEdit");
		mv.addObject("message", banner);

		String msg = PageTools.alertCloseFraRefresh("修改首页banner", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 添加首页banner--初使化
	 */
	@RequestMapping(value = "/front/message/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/messageAdd");
		return mv;
	}

	/**
	 * 添加首页banner--保存
	 */
	@RequestMapping(value = "/front/message/add/save")
	public ModelAndView addSave(@ModelAttribute Message banner) {	
		Integer res = messageServ.addMessage(banner);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/messageAdd");
		mv.addObject("message", banner);

		String msg = PageTools.alertCloseFraRefresh("添加首页banner", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 查看首页banner信息
	 */
	@ResponseBody
	@RequestMapping(value = "/front/message/view/{id}")
	public PageJosnMessage view(@PathVariable(value = "id") Integer id) {
		Message banner = messageServ.getMessage(id);
		PageJosnMessage msg = new PageJosnMessage();
		msg.putData("message", banner);
		return msg;
	}

	/**
	 * 启用、禁用首页banner
	 */
	@ResponseBody
	@RequestMapping(value = "/front/message/publish")
	public PageJosnMessage publish(@RequestParam int id, @RequestParam int isEnable) {
		Integer res = messageServ.publish(id, isEnable == 1);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}


	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/front/message/delete/{id}")
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
		Integer res = messageServ.deleteMessage(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

}
